<?php
namespace cash\Controller;
use Think\Controller;
class UserController extends Controller {

	public function login(){
		if(is_login()){
			redirect('/recharge/index?res=0-1');
		}else{
    		$this->display('user/login');
		}
    }
    
	/*
	 * 校验登陆
	 */
    public function checklogin(){
    	$userMsg = M('ResEntity')
    			 ->where('username = "'.trim(I('post.username')).'" AND is_cash = 1')->find();
		if(!$userMsg){
			jsonRespons('0','帐号不存在');
		}
    	if($userMsg['password'] == md5(I('post.passwd').$userMsg['salt'])){
    		if(!$userMsg['status']){
    			jsonRespons('0','您的帐号已冻结，请联系客服！');
    		}
    		cookie('_pdlfctunion',md5(time()));
    		session('loginUid',$userMsg['rid']);
    		session('loginName',$userMsg['username']);
    		jsonRespons('1','登陆成功');
    	}else{
    		jsonRespons('0','帐号密码有误，请重试');
    	}
    }
    
	/*
	 * 帐号退出
	 */
    public function loginout(){
    	session(cookie('PHPSESSID'),null);
    	session('loginName',null);
    	session('loginUid',null);
    	cookie('PHPSESSID',null);
    	session_destroy();

    	redirect('/user/login');
    }

	/*
	 * 个人中心简介
	 */
	public function usercenter()
	{
		//个人页面渲染
		$this->display('user/usercenter');
	}

    /*
     * 个人中心资料编辑
     */
    public function userUpdate(){
    	if(I('post.update')){
    		if(I('post.password')){
    			$salt = randstr(5);
    			$data['password'] = md5(I('post.password').$salt);
    			$data['salt'] = $salt;
    		}
    		$data['email'] = I('post.email');
    		M('ResEntity')->where(array('rid'=>session('loginUid')))->setField($data);
    		$this->showtips('信息编辑成功');
    	}
    }
}